package com.springboot.hiresystem.controller;

import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.springboot.hiresystem.domain.SearchHistory;
import com.springboot.hiresystem.dto.req.SearchHistoryQueryRequestDTO;
import com.springboot.hiresystem.dto.req.SearchHistoryUpdateRequestDTO;
import com.springboot.hiresystem.dto.resp.PageResponse;
import com.springboot.hiresystem.dto.resp.Result;
import com.springboot.hiresystem.service.SearchHistoryService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/searchHistory")
@Slf4j
public class SearchHistoryController {

    private final SearchHistoryService searchHistoryService;

    public SearchHistoryController(SearchHistoryService searchHistoryService) {
        this.searchHistoryService = searchHistoryService;
    }

    // 分页查询
    @GetMapping("/list")
    public Result<PageResponse<SearchHistory>> list(SearchHistoryQueryRequestDTO request) {
        QueryWrapper<SearchHistory> queryWrapper = new QueryWrapper<>();
        if (request.getUserId() != null) {
            queryWrapper.eq("user_id", request.getUserId());
        }
        queryWrapper.orderByAsc("search_id");

        Page<SearchHistory> page = new Page<>(request.getPage(), request.getSize());
        Page<SearchHistory> resultPage = searchHistoryService.page(page, queryWrapper);

        PageResponse<SearchHistory> response = new PageResponse<>();
        response.setTotal(resultPage.getTotal());
        response.setList(resultPage.getRecords());

        return Result.success(response);
    }

    // 条件查询
    @PostMapping("/query")
    public Result<List<SearchHistory>> query(@RequestBody SearchHistoryQueryRequestDTO request) {
        QueryWrapper<SearchHistory> queryWrapper = new QueryWrapper<>();

        if (request.getUserId() != null) {
            queryWrapper.eq("user_id", request.getUserId());
        }

        List<SearchHistory> searchHistories = searchHistoryService.list(queryWrapper);
        return Result.success(searchHistories);
    }

    // 更新或添加
    @PostMapping("/update")
    public Result<String> update(@RequestBody SearchHistoryUpdateRequestDTO request) {
        SearchHistory searchHistory =  BeanUtil.copyProperties(request, SearchHistory.class);
        System.out.println(request.getKeyWords());
        System.out.println(searchHistory.getKeyWords());
        if (request.getSearchId() != null && request.getSearchId() > 0) {
            searchHistoryService.updateById(searchHistory);
            return Result.success("更新成功");
        } else {
            if (!searchHistoryService.save(searchHistory)) {
                return Result.failure(500, "添加失败");
            }
            return Result.success("新增成功");
        }
    }

    // 删除
    @PostMapping("/delete")
    public Result<String> delete(@RequestBody SearchHistory searchHistory) {
        searchHistoryService.removeById(searchHistory.getSearchId());
        return Result.success("删除成功");
    }
}
